﻿'
'Scroller Plugin for LCDSmartie
'##############################
'Allows the user to scroll text in different styles in LCDSmartie
'
'v 0.9.0 20.07.2012
'by Oliver Tomkins (freeze@gmx.de)
'

Public Class LCDSmartie

    'Global variables
    'Needed so that they are not reset between calls

    'Needed to read dymanic configuration
    Public iRefreshInt As Int32 = System.Configuration.ConfigurationManager.AppSettings("Scroller.RefreshInt")

    Public iScroll1 As Int32
    Public sText1 As String
    Public iCycleWait1 As Int32

    Public iScroll2 As Int32
    Public sText2 As String
    Public iCycleWait2 As Int32

    Public iScroll3 As Int32
    Public sText3 As String
    Public iCycleWait3 As Int32

    Public iScroll4 As Int32
    Public sText4 As String
    Public iCycleWait4 As Int32

    Public iScroll5 As Int32
    Public sText5 As String
    Public iCycleWait5 As Int32

    'Not used yet

    Public iScroll6 As Int32
    Public sText6 As String
    Public iCycleWait6 As Int32

    Public iScroll7 As Int32
    Public sText7 As String
    Public iCycleWait7 As Int32

    Public iScroll8 As Int32
    Public sText8 As String
    Public iCycleWait8 As Int32

    Public iScroll9 As Int32
    Public sText9 As String
    Public iCycleWait9 As Int32

    Public iScroll10 As Int32
    Public sText10 As String
    Public iCycleWait10 As Int32

    Public Function function1(ByVal param1 As String, ByVal param2 As String) As String

        Dim sText As String
        Dim iLCDSize As Int32
        Dim iParameter As New List(Of String)(param2.Split("#"))

        sText = param1

        If iParameter.Count = 0 Then
            Return "No valid Param2!"
        ElseIf iParameter.Count = 1 Then
            iParameter.Add("1")
        End If

        'Don't scroll if received text ist smaller than the LCD-Size
        If sText.Length <= FormatNumber(iParameter(0)) Then
            Return sText
        End If

        'Set normal scrolling as default
        If iParameter.Count = 2 Then
            iParameter.Add("0")
        End If

        'Add spacing character
        If iParameter(2) <> "0" Then
            sText = sText & iParameter(2)
        End If

        Select Case iParameter(1)


            Case 1
                'Normal scroll

                iLCDSize = FormatNumber(iParameter(0))

                If sText = sText1 Then  'Scroll
                    Return MoveText(iScroll1, sText1, iLCDSize)
                Else                    'Store new text, scroll then
                    sText1 = sText
                    iScroll1 = 0
                    Return MoveText(iScroll1, sText1, iLCDSize)
                End If


            Case 2
                'Scroll only once

                iLCDSize = FormatNumber(iParameter(0))

                If sText = sText1 Then  'Scroll
                    If iScroll1 = sText1.Length Then
                        Return sText1.Substring(0, iLCDSize)
                    Else
                        Return MoveText(iScroll1, sText1, iLCDSize)
                    End If
                Else
                    sText1 = sText
                    iScroll1 = 0
                    Return MoveText(iScroll1, sText1, iLCDSize)
                End If

            Case 3
                'Scroll and sleep for a short while when finished

                'Missing parameter
                If iParameter.Count = 3 Then
                    Return "Specify valid WaitCycle-Value"
                End If

                iLCDSize = FormatNumber(iParameter(0))

                If sText = sText1 Then
                    If iCycleWait1 <= 0 Then
                        'Reset Counter
                        iCycleWait1 = FormatNumber(iParameter(3))
                        Return MoveText(iScroll1, sText1, iLCDSize)
                    End If
                    If iScroll1 = sText1.Length Then
                        'Wait specified cycles
                        iCycleWait1 = iCycleWait1 - 1
                        Return sText1.Substring(0, iLCDSize)
                    Else
                        Return MoveText(iScroll1, sText1, iLCDSize)
                    End If
                Else
                    'Do do anything special

                    sText1 = sText
                    iScroll1 = 0
                    iCycleWait1 = FormatNumber(iParameter(3))

                    Return MoveText(iScroll1, sText1, iLCDSize)
                End If


            Case Else

                Return "No valid Param2!"

        End Select

    End Function


    Public Function function2(ByVal param1 As String, ByVal param2 As String) As String

        Dim sText As String
        Dim iLCDSize As Int32
        Dim iParameter As New List(Of String)(param2.Split("#"))

        sText = param1

        If iParameter.Count = 0 Then
            Return "No valid Param2!"
        ElseIf iParameter.Count = 1 Then
            iParameter.Add("1")
        End If

        If sText.Length <= FormatNumber(iParameter(0)) Then
            Return sText
        End If

        If iParameter.Count = 2 Then
            iParameter.Add("0")
        End If

        If iParameter(2) <> "0" Then
            sText = sText & iParameter(2)
        End If

        Select Case iParameter(1)

            Case 1

                iLCDSize = FormatNumber(iParameter(0))

                If sText = sText2 Then
                    Return MoveText(iScroll2, sText2, iLCDSize)
                Else
                    sText2 = sText
                    iScroll2 = 0
                    Return MoveText(iScroll2, sText2, iLCDSize)
                End If

            Case 2

                iLCDSize = FormatNumber(iParameter(0))

                If sText = sText2 Then
                    If iScroll2 = sText2.Length Then
                        Return sText2.Substring(0, iLCDSize)
                    Else
                        Return MoveText(iScroll2, sText2, iLCDSize)
                    End If
                Else
                    sText2 = sText
                    iScroll2 = 0
                    Return MoveText(iScroll2, sText2, iLCDSize)
                End If

            Case 3

                If iParameter.Count = 3 Then
                    Return "Specify valid WaitCycle-Value"
                End If

                iLCDSize = FormatNumber(iParameter(0))

                If sText = sText2 Then
                    If iCycleWait2 <= 0 Then
                        iCycleWait2 = FormatNumber(iParameter(3))
                        Return MoveText(iScroll2, sText2, iLCDSize)
                    End If
                    If iScroll2 = sText2.Length Then
                        iCycleWait2 = iCycleWait2 - 1
                        Return sText2.Substring(0, iLCDSize)
                    Else
                        Return MoveText(iScroll2, sText2, iLCDSize)
                    End If
                Else

                    sText2 = sText
                    iScroll2 = 0
                    iCycleWait2 = FormatNumber(iParameter(3))

                    Return MoveText(iScroll1, sText2, iLCDSize)
                End If


            Case Else

                Return "No valid Param2!"

        End Select

    End Function

    Public Function function3(ByVal param1 As String, ByVal param2 As String) As String

        Dim sText As String
        Dim iLCDSize As Int32
        Dim iParameter As New List(Of String)(param2.Split("#"))

        sText = param1

        If iParameter.Count = 0 Then
            Return "No valid Param2!"
        ElseIf iParameter.Count = 1 Then
            iParameter.Add("1")
        End If

        If sText.Length <= FormatNumber(iParameter(0)) Then
            Return sText
        End If

        If iParameter.Count = 2 Then
            iParameter.Add("0")
        End If

        If iParameter(2) <> "0" Then
            sText = sText & iParameter(2)
        End If

        Select Case iParameter(1)

            Case 1

                iLCDSize = FormatNumber(iParameter(0))

                If sText = sText3 Then
                    Return MoveText(iScroll3, sText3, iLCDSize)
                Else
                    sText3 = sText
                    iScroll3 = 0
                    Return MoveText(iScroll3, sText3, iLCDSize)
                End If

            Case 2

                iLCDSize = FormatNumber(iParameter(0))

                If sText = sText3 Then
                    If iScroll3 = sText3.Length Then
                        Return sText3.Substring(0, iLCDSize)
                    Else
                        Return MoveText(iScroll3, sText3, iLCDSize)
                    End If
                Else
                    sText3 = sText
                    iScroll3 = 0
                    Return MoveText(iScroll3, sText3, iLCDSize)
                End If

            Case 3

                If iParameter.Count = 3 Then
                    Return "Specify valid WaitCycle-Value"
                End If

                iLCDSize = FormatNumber(iParameter(0))

                If sText = sText3 Then
                    If iCycleWait3 <= 0 Then
                        iCycleWait3 = FormatNumber(iParameter(3))
                        Return MoveText(iScroll3, sText3, iLCDSize)
                    End If
                    If iScroll3 = sText3.Length Then
                        iCycleWait3 = iCycleWait3 - 1
                        Return sText3.Substring(0, iLCDSize)
                    Else
                        Return MoveText(iScroll3, sText3, iLCDSize)
                    End If
                Else

                    sText3 = sText
                    iScroll3 = 0
                    iCycleWait3 = FormatNumber(iParameter(3))

                    Return MoveText(iScroll1, sText3, iLCDSize)
                End If


            Case Else

                Return "No valid Param2!"

        End Select

    End Function

    Public Function function4(ByVal param1 As String, ByVal param2 As String) As String

        Dim sText As String
        Dim iLCDSize As Int32
        Dim iParameter As New List(Of String)(param2.Split("#"))

        sText = param1

        If iParameter.Count = 0 Then
            Return "No valid Param2!"
        ElseIf iParameter.Count = 1 Then
            iParameter.Add("1")
        End If

        If sText.Length <= FormatNumber(iParameter(0)) Then
            Return sText
        End If

        If iParameter.Count = 2 Then
            iParameter.Add("0")
        End If

        If iParameter(2) <> "0" Then
            sText = sText & iParameter(2)
        End If

        Select Case iParameter(1)

            Case 1

                iLCDSize = FormatNumber(iParameter(0))

                If sText = sText4 Then
                    Return MoveText(iScroll4, sText4, iLCDSize)
                Else
                    sText4 = sText
                    iScroll4 = 0
                    Return MoveText(iScroll4, sText4, iLCDSize)
                End If

            Case 2

                iLCDSize = FormatNumber(iParameter(0))

                If sText = sText4 Then
                    If iScroll4 = sText4.Length Then
                        Return sText4.Substring(0, iLCDSize)
                    Else
                        Return MoveText(iScroll4, sText4, iLCDSize)
                    End If
                Else
                    sText4 = sText
                    iScroll4 = 0
                    Return MoveText(iScroll4, sText4, iLCDSize)
                End If

            Case 3

                If iParameter.Count = 3 Then
                    Return "Specify valid WaitCycle-Value"
                End If

                iLCDSize = FormatNumber(iParameter(0))

                If sText = sText4 Then
                    If iCycleWait4 <= 0 Then
                        iCycleWait4 = FormatNumber(iParameter(3))
                        Return MoveText(iScroll4, sText4, iLCDSize)
                    End If
                    If iScroll4 = sText4.Length Then
                        iCycleWait4 = iCycleWait4 - 1
                        Return sText4.Substring(0, iLCDSize)
                    Else
                        Return MoveText(iScroll4, sText4, iLCDSize)
                    End If
                Else

                    sText4 = sText
                    iScroll4 = 0
                    iCycleWait4 = FormatNumber(iParameter(3))

                    Return MoveText(iScroll1, sText4, iLCDSize)
                End If


            Case Else

                Return "No valid Param2!"

        End Select

    End Function

    Public Function function5(ByVal param1 As String, ByVal param2 As String) As String

        Dim sText As String
        Dim iLCDSize As Int32
        Dim iParameter As New List(Of String)(param2.Split("#"))

        sText = param1

        If iParameter.Count = 0 Then
            Return "No valid Param2!"
        ElseIf iParameter.Count = 1 Then
            iParameter.Add("1")
        End If

        If sText.Length <= FormatNumber(iParameter(0)) Then
            Return sText
        End If

        If iParameter.Count = 2 Then
            iParameter.Add("0")
        End If

        If iParameter(2) <> "0" Then
            sText = sText & iParameter(2)
        End If

        Select Case iParameter(1)

            Case 1

                iLCDSize = FormatNumber(iParameter(0))

                If sText = sText5 Then
                    Return MoveText(iScroll5, sText5, iLCDSize)
                Else
                    sText5 = sText
                    iScroll5 = 0
                    Return MoveText(iScroll5, sText5, iLCDSize)
                End If

            Case 2

                iLCDSize = FormatNumber(iParameter(0))

                If sText = sText5 Then
                    If iScroll5 = sText5.Length Then
                        Return sText5.Substring(0, iLCDSize)
                    Else
                        Return MoveText(iScroll5, sText5, iLCDSize)
                    End If
                Else
                    sText5 = sText
                    iScroll5 = 0
                    Return MoveText(iScroll5, sText5, iLCDSize)
                End If

            Case 3

                If iParameter.Count = 3 Then
                    Return "Specify valid WaitCycle-Value"
                End If

                iLCDSize = FormatNumber(iParameter(0))

                If sText = sText5 Then
                    If iCycleWait5 <= 0 Then
                        iCycleWait5 = FormatNumber(iParameter(3))
                        Return MoveText(iScroll5, sText5, iLCDSize)
                    End If
                    If iScroll5 = sText5.Length Then
                        iCycleWait5 = iCycleWait5 - 1
                        Return sText5.Substring(0, iLCDSize)
                    Else
                        Return MoveText(iScroll5, sText5, iLCDSize)
                    End If
                Else

                    sText5 = sText
                    iScroll5 = 0
                    iCycleWait5 = FormatNumber(iParameter(3))

                    Return MoveText(iScroll1, sText5, iLCDSize)
                End If


            Case Else

                Return "No valid Param2!"

        End Select

    End Function


    Private Function MoveText(ByRef Counter As Int32, ByVal Text As String, ByVal Size As Int32)

        Dim sTempString

        'Reset if scrolling has completed
        If Counter >= Text.Length Then

            Counter = 0

        End If

        sTempString = Text.Substring(Counter, Text.Length - Counter)
        sTempString = sTempString & Text.Substring(0, Counter)
        Counter = Counter + 1
        Return sTempString.ToString.Substring(0, Size)

    End Function

    '
    ' Define the minimum interval that a screen should get fresh data from our plugin.
    '
    Public Function GetMinRefreshInterval() As Integer

        Return iRefreshInt

    End Function


End Class

